using System;
using System.Collections.Generic;
using System.Text;
using Frontends.Controls;
using Core.Items;
using System.Windows.Forms;
using Core;
using Frontends.Nodes;

namespace Frontends.Controller
{
    public class ToDoPersonalFormController
    {
        private ToDoPersonalForm _mainForm;
        private ToDoItemController _controller;
        private ToDoItemTreeController _treeController;
        

        public ToDoPersonalFormController(ToDoPersonalManager manager)
        {
            _mainForm = new ToDoPersonalForm();
            _controller = new ToDoItemController(_mainForm._toDoItemControl);
            _treeController = new ToDoItemTreeController(_mainForm._treeView, manager);
         
        }

        internal void Initialize()
        {
            _mainForm._tsbuttonAdd.Click += delegate
            {
                ToDoItem item = new ToDoItem("New Item");
                ToDoItemTreeNode node = new ToDoItemTreeNode(item);
                TreeNode selectedNode = _mainForm._treeView.SelectedNode;
                if (selectedNode != null && selectedNode is ToDoItemTreeNode)
                {
                    _treeController.AddNode(node, (selectedNode as ToDoItemTreeNode).Item.Id);
                }
                else
                {
                    _treeController.AddNode(node, Guid.Empty);
                }
                _controller.Display(item);
            };

            _treeController.Initialize();
                       

        }
        
        public static DialogResult ShowDialog(ToDoPersonalManager manager)
        {
            ToDoPersonalFormController controller = new ToDoPersonalFormController(manager);
            controller.Initialize();
            return controller._mainForm.ShowDialog();
        }

    }
}
